package com.egene.tw.activity.member;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egene.tw.R;
import com.egene.tw.api.EgeneApi;
import com.egene.tw.base.BaseRecycleViewFragment;
import com.egene.tw.base.RecycleBaseAdapter;
import com.egene.tw.dao.UserDao;
import com.egene.tw.model.User;
import com.egene.tw.model.UserCenterItem;
import com.egene.tw.model.UserCenterList;
import com.egene.tw.utils.JsonUtil;
import com.egene.tw.utils.MToast;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.apache.http.Header;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@EFragment
public class KnowYourselfFragment extends BaseRecycleViewFragment {
    @Bean
    UserDao userDao;

    ImageView ivLineBg;
    ImageView ivLine;
    TextView tvHealthSource;

    int mParentId = -1;

    @Override
    protected void initViews(View view) {
        super.initViews(view);
    }


    @Override
    protected RecycleBaseAdapter getListAdapter() {
        View head = initHead();
        return new KnowRecycleAdapter(head);
    }

    /**
     * RecycleView 头部
     * @return
     */
    private View initHead() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.head_know_yourself,null,false);
        ivLineBg = (ImageView) view.findViewById(R.id.ivLineBg);
        ivLine = (ImageView) view.findViewById(R.id.ivLine);
        tvHealthSource = (TextView) view.findViewById(R.id.tvHealthSource);
        return view;
    }

    @Override
    protected void sendRequestData() {
        fetchList();
    }

    public void refresh(int id){
        mParentId = id;
        refresh();
    }

    public void fetchList() {
        String u = userDao.getUser().getUserID();
        EgeneApi.getUserCenterList(u, mParentId,new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                UserCenterList result = JsonUtil.parse(responseBody, UserCenterList.class);
                if(result != null){
                    if(result.isSuccess()){
                        updateList(result.getCategorylist());
                        updateHealthLine(result.getHealthyvalue().intValue());
                    }else{
                        MToast.show(result.getMessage());
                    }
                }else{
                    MToast.show(R.string.server_error);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                MToast.show(R.string.no_network);
            }

            @Override
            public void onFinish() {
                executeOnLoadFinish();
                super.onFinish();
            }
        });
    }

    @Override
    public void loadMore() {
//        super.loadMore();
    }

    @Override
    public void refresh() {
        super.refresh();
    }

    /**
     * 更新认识自己列表
     * @param list
     */
    void updateList(List<UserCenterItem> list) {
        if(mCurrentPage == START_PAGE_INDEX) {
            mAdapter.clear();
        }
        mAdapter.addData(list);
    }

    /**
     * 更新健康达人值
     *
     * @param values
     */
    void updateHealthLine(int values) {
        User user = userDao.getUser();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date birthDay = null;
        try {
            birthDay = simpleDateFormat.parse(user.getBirthdayString());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //16年的毫秒数
        long dateSpace = 31536000000l * 16;
        Date data = new Date(System.currentTimeMillis() - dateSpace);
        if(birthDay != null){

            //用16年前的今天与生日比较
            if (data.after(birthDay)) {
                tvHealthSource.setText(getActivity().getString(R.string.health_source) + values);
            }else{
                tvHealthSource.setText(getActivity().getString(R.string.child_health_source) + values);
            }
        }else{
            tvHealthSource.setText(getActivity().getString(R.string.health_source) + values);
        }


//        tvHealthSource.setText(getActivity().getString(R.string.health_source) + values);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) ivLine.getLayoutParams();
        lp.width = ivLineBg.getWidth() * values / 100;
        ivLine.setLayoutParams(lp);
    }

    @Override
    public void onItemClick(View v,int position) {
        UserCenterItem item = (UserCenterItem) mAdapter.getItem(position - 1);
        if(item.ischecked()){
//            Intent intent = new Intent(getActivity(),KnowDetailActivity.class);
//            startActivity(intent);
            KnowDetailActivity_.intent(getActivity()).userCenterItem(item).start();
        }else {
            MToast.show(R.string.no_detail);
        }

    }
}
